﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class web_nhatuyendung_taikhoan : System.Web.UI.Page
{
    public string mess = "";
    public objDoanhNghiep objNTD;
    public string NTDtaikhoan;
    protected void Page_Load(object sender, EventArgs e)
    {
        this.Title = "Nhà tuyển dụng " + ConfigurationManager.AppSettings["centername"];
        if (Session["NTDuser"] == null)
        {
            Response.Redirect("nhatuyendung.aspx");
        }
        else {
            NTDtaikhoan = Session["NTDtaikhoan"].ToString();
        }
        if (!IsPostBack)
        {
            LoadTP();
            LoadQG();
            LoadSoHuu();
            LoadKCN();
            LoadNTD();

            
        }
    }
    
    ///
    /// load dữ liệu
    ///
    protected void LoadNTD() {

        NhaTuyenDungCommon ntdComm = new NhaTuyenDungCommon();
        objNTD = ntdComm.getNTD(NTDtaikhoan);
        TextBox1.Text = objNTD.ten_doanh_nghiep;
        txt_chuc_vu.Text = objNTD.chuc_vu;
        txt_dia_chi_chi_tiet.Text = objNTD.dia_chi_chitiet;
        txt_dien_thoai.Text = objNTD.dien_thoai;
        txt_fax.Text = objNTD.fax;
        txt_ma_so_thue.Text = objNTD.ma_so_thue;
        txt_nam_thanh_lap.Text = objNTD.nam_thanh_lap;
        list_nganh_kd.Items.Clear();
        foreach (ListItem _li in objNTD.nganh_kd)
        {
            list_nganh_kd.Items.Add(_li);
        }
        txt_nguoi_dai_dien.Text = objNTD.nguoi_lien_he;
        txt_skype.Text = objNTD.skype;
        txt_so_lao_dong.Text = objNTD.so_lao_dong.ToString();
        txt_tenviettat.Text = objNTD.ten_tat;
        txt_website.Text = objNTD.website;
        txt_yahoo.Text = objNTD.yahoo;

        txt_hinhanh.ImageUrl = "~/Uploads/Images/" + objNTD.hinhanh;

        cbb_khu_cn.Items.FindByValue(objNTD.ma_khu_cn).Selected = true;
        cbb_kieu_so_huu.Items.FindByValue(objNTD.ma_so_huu).Selected = true;
        cbb_quoc_gia.Items.FindByValue(objNTD.ma_quoc_gia).Selected = true;
        txt_dia_chi_THX_hidden.Value = objNTD.ma_dia_chi;
        txt_diachi_thx.Text = NhaTuyenDungCommon.getDiaChiTHXString(objNTD.ma_dia_chi);
        

    }

    /// <summary>
    /// load danh mục
    /// </summary>
    protected void LoadTP()
    {
        Common comm = new Common();

        string sqlComm = "Select ma_dia_chi, ten_dia_chi from DanhMuc_dia_chi where muc=0 order by ten_dia_chi ";
        DataTable dtt = comm.getDataTable(sqlComm);
        for (int i = 0; i < dtt.Rows.Count; i++)
        {
            cbb_tinhthanh.Items.Add(new ListItem(dtt.Rows[i]["ten_dia_chi"].ToString(), dtt.Rows[i]["ma_dia_chi"].ToString()));
        }
    }
    protected void LoadQG()
    {
        Common comm = new Common();

        string sqlComm = "Select ma_quoc_gia, ten_quoc_gia from DanhMuc_quoc_gia  order by uu_tien desc ";
        DataTable dtt = comm.getDataTable(sqlComm);
        for (int i = 0; i < dtt.Rows.Count; i++)
        {
            cbb_quoc_gia.Items.Add(new ListItem(dtt.Rows[i]["ten_quoc_gia"].ToString(), dtt.Rows[i]["ma_quoc_gia"].ToString()));
        }
    }
    protected void LoadSoHuu()
    {
        Common comm = new Common();

        string sqlComm = "Select ma_so_huu, ten_so_huu from DanhMuc_so_huu   ";
        DataTable dtt = comm.getDataTable(sqlComm);
        for (int i = 0; i < dtt.Rows.Count; i++)
        {
            cbb_kieu_so_huu.Items.Add(new ListItem(dtt.Rows[i]["ten_so_huu"].ToString(), dtt.Rows[i]["ma_so_huu"].ToString()));
        }
    }
    protected void LoadKCN()
    {
        Common comm = new Common();

        string sqlComm = "Select ma_khu_cn, ten_khu_cn from DanhMuc_khu_cong_nghiep   ";
        DataTable dtt = comm.getDataTable(sqlComm);
        for (int i = 0; i < dtt.Rows.Count; i++)
        {
            cbb_khu_cn.Items.Add(new ListItem(dtt.Rows[i]["ten_khu_cn"].ToString(), dtt.Rows[i]["ma_khu_cn"].ToString()));
        }
    }
    public string SaveFile(FileUpload file, string folder, string name)
    {
        try
        {
            if ((file.PostedFile != null) && (file.PostedFile.InputStream.Length > 0))
            {
                char[] slp = new char[] { '.' };
                string fileEx = file.PostedFile.FileName.Split(slp)[file.PostedFile.FileName.Split(slp).Length - 1].ToString();
                if (fileEx.ToLower().Equals("jpg") || fileEx.ToLower().Equals("png") || fileEx.ToLower().Equals("gif") || fileEx.ToLower().Equals("jpge"))
                {

                    string fileName = "NTD_" + name + "_logo." + fileEx;
                    string saveName = Server.MapPath(folder) + fileName;
                    file.SaveAs(saveName);
                    return fileName;
                }
                else
                    return "";
            }
            else
                return "";
        }
        catch (Exception)
        {

            return "";
        }
    }
    protected void btn_editPass_Click(object sender, EventArgs e)
    {
        Common comm = new Common();
        NhaTuyenDungCommon ntdComm = new NhaTuyenDungCommon();
        NTDtaikhoan = Session["NTDtaikhoan"].ToString();
        if (ntdComm.login(NTDtaikhoan, txt_mk_cu.Text))
        {
            string sqlComm = "Update DoanhNghiep_doanh_nghiep set mat_khau = '" + MaHoa.EncryptData(txt_mk_moi.Text) + "'"
				+ " where tai_khoan='"+NTDtaikhoan+"'";
            comm.execNoneQuery(sqlComm);
            Response.Redirect("notice.aspx?backurl=NTD_Home");
        }
    }
    protected void btn_dang_xuat_Click(object sender, EventArgs e)
    {
        Session.Abandon();
        Response.Redirect("notice.aspx?backurl=NTD_Home");
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
    
        NhaTuyenDungCommon ntdComm = new NhaTuyenDungCommon();
        objNTD = ntdComm.getNTD(NTDtaikhoan);
        string thumb = objNTD.hinhanh;

        if(txt_hinh_anh.HasFile)
        thumb = SaveFile(txt_hinh_anh, "~/Uploads/Images/", objNTD.tai_khoan);
        if (thumb.Length == 0) thumb = objNTD.hinhanh;
	objNTD.ten_doanh_nghiep=TextBox1.Text;
        objNTD.chuc_vu = txt_chuc_vu.Text.Trim();
        objNTD.dia_chi_chitiet = txt_dia_chi_chi_tiet.Text.Trim();
        objNTD.dien_thoai = txt_dien_thoai.Text.Trim();
        objNTD.fax = txt_fax.Text.Trim();
        objNTD.hinhanh = thumb;
        objNTD.ma_dia_chi = txt_dia_chi_THX_hidden.Value.Trim();
        objNTD.ma_khu_cn = cbb_khu_cn.SelectedValue;
        objNTD.ma_quoc_gia = cbb_quoc_gia.SelectedValue;
        objNTD.ma_so_huu = cbb_kieu_so_huu.SelectedValue;
        objNTD.ma_so_thue = txt_ma_so_thue.Text.Trim();
        objNTD.nam_thanh_lap = txt_nam_thanh_lap.Text.Trim();
        objNTD.nguoi_lien_he = txt_nguoi_dai_dien.Text.Trim();
        objNTD.yahoo = txt_yahoo.Text.Trim();
        objNTD.skype = txt_skype.Text.Trim();
        try
        {
            objNTD.so_lao_dong = Convert.ToInt32(txt_so_lao_dong.Text.Trim());
        }
        catch (Exception)
        {

            objNTD.so_lao_dong = 0;
        }

        objNTD.ten_tat = txt_tenviettat.Text.Trim();
        objNTD.website = txt_website.Text;

        objNTD.nganh_kd = new ListItemCollection();

        char[] splChar = new char[] { '#' };

        for (int i = 0; i < list_nganh_kd_hidden.Value.Split(splChar).Length - 2; i += 2)
        {

            objNTD.nganh_kd.Add(new ListItem(list_nganh_kd_hidden.Value.Split(splChar)[i + 1], list_nganh_kd_hidden.Value.Split(splChar)[i]));
        }

        ntdComm.updateNTD(objNTD);
        LoadNTD();
        


    }
}
